<template>
    <table border="1">
        <tr>
            <td colspan="2" style="text-align:center;font-size:30px;">房号信息-修改</td>
        </tr>
        <tr>
            <td>房间名称</td>
            <td>
                <select v-model="Info.T_Id">
                    <option :value="0">--请选择--</option>
                    <option v-for="item in Type" :value="item.T_Id">{{item.T_Name}}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>房号</td>
            <td>
                <input type="text" v-model="Info.N_Name"/>
            </td>
        </tr>
        <tr>
            <td>备注</td>
            <td>
                <textarea cols="20" rows="4" v-model="Info.N_Remark"></textarea>
            </td>
        </tr>
        <tr>
            <td>状态</td>
            <td>
                <input type="radio" :value="true" v-model="Info.N_State"/>上架
                <input type="radio" :value="false" v-model="Info.N_State"/>下架
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align:center;">
                <input type="button" value="修改" @click="addnumber"/>
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue'
import axios from 'axios'
import {useRouter,useRoute} from 'vue-router'

//创建路由的实例
const router=useRouter();
const route=useRoute();

//生命周期钩子
onMounted(()=>{
    GetType();
    FanTian();
})

//下拉框
const Type=ref([{
    T_Id:0,
    T_Name:""
}])

//显示下拉框
const GetType=()=>{
    axios.get("https://localhost:7205/api/RoomNumber/GetType")
    .then(res=>{
        Type.value=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}

//响应式数据
const Info=ref({
    N_Id:route.params.id,
    N_Name:"",
    N_Remark:"",
    N_State:true,
    T_Id:0
})

//反填
const FanTian=()=>{
    axios.get("https://localhost:7205/api/RoomNumber/GetNumber",{
        params:{
            Nid:route.params.id
        }
    })
    .then(res=>{
        Info.value=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}

//保存数据
const addnumber=()=>{
    if(Info.value.T_Id==0)
    {
        alert("房型名称不能为空");
        return;
    }
    if(Info.value.N_Name=="")
    {
        alert("房号名称不能为空");
        return;
    }
    axios.put("https://localhost:7205/api/RoomNumber/UpdRoomNumber",Info.value)
    .then(res=>{
        if(res.data>0)
        {
            alert("修改成功");
            router.push("/ShowRoomNumber");
        }
        else if(res.data==-1)
        {
            alert("当前房型已经存在");
            return;
        }
        else{
            alert("修改失败");
        }
    })
}
</script>